#include "Login.h"
#include "Database.h"
#ifndef DATABASE_FILE
#define DATABASE_FILE "Database.Assn2"

Login::Login() {
    loginTries = 0;
    loginIndex = 0;
}

int Login::checkValidUserPass(string id, string pass) {
    int count = 0;
    
    Database *db;
    db = new Database("Database.Assn2");
    vector<vector<string> > result = db->query("SELECT * FROM users;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it)
    {
        count++;
        vector<string> row = *it;
        if(id == row.at(0)){
            if(pass == row.at(1)){
                cout << "Successfully log in" << endl << endl;
                loginIndex = count;
                role = row.at(2);
                username = row.at(0);
                return 1;   
            } else {
                cout << "Invalid Password" << endl << endl;
                return -1;
            }
        }
    }
    cout << "Invalid ID" << endl << endl;
    return -1;
    db->close();
    
}

bool checkPassword(string pass) {
    
    //Check if there are 8 chars
    if (pass.length() < 8) {
        cout << endl << "New Password has to be at least 8 chars long." << endl;
        return false;
    }

    int numCount = 0;
    int alphaCount = 0;

    //Go through every character in password and count num of alphabet and num
    for (int i = 0; i < pass.length(); i++) {
        if (isalpha(pass[i])) {
            alphaCount++;
        } else if (isdigit(pass[i])) {
            numCount++;
        }
    }
    //If there is at least 1 alpha and num
    if (numCount > 0 && alphaCount > 0) {
        cout << endl << "New password valid." << endl;
        return true;
    }

    //If all else fails means, it did not contain 1 num and 1 alpha
    cout << endl << "New password has to contain 1 number and 1 alpha" << endl;
    return false;
}

void Login::changePassword() {
    
    string password;
    cout << endl << "Proceeding to Change Password....." << endl;
    while (1) { // keeps looping, only exits when password updated
        cout << "Please enter new password: ";
        cin >> password;
        cin.clear();
        cin.ignore(100, '\n');
        //Check if password is valid
            if (checkPassword(password)) {
                cout << "Updating Password..." << endl;
                
                Database *db;
                db = new Database("Database.Assn2");
                
                string sqlstatement;
                sqlite3 *db1;
                sqlite3_stmt *stmt;
                vector<vector<string> > result = db->query("SELECT * FROM users;");
                for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it)
                {
                    vector<string> row = *it;   
                    if(username == row.at(0)){
                        sqlstatement = "UPDATE users SET password='"+password+"' WHERE username='"+username+"'";                     
                       
                        if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                        {
                            sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                            sqlite3_step(stmt);
                            sqlite3_finalize(stmt);
                            sqlite3_close(db1);
                        }
                
                       }
                    }return;
                }
        
            }
    
}

int Login::getLogin() {
    string id, pass;
    cout << "Please enter login ID and Password.." << endl;
    cout << "Login ID: ";
    cin >> id;
    cin.clear();
    cin.ignore(100, '\n');

    cout << "Password: ";
    cin >> pass;
    cin.clear();
    cin.ignore(100, '\n');

    return checkValidUserPass(id, pass); //return -1 for failure, 1 for success
}

void Login::updateDetails() {

}

#endif